/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mp.weixin.WXpublic.sendkefu;

import java.util.List;
import mp.weixin.WXpublic.BaseSendKefuMessage;
import org.codehaus.jackson.node.ArrayNode;
import org.codehaus.jackson.node.JsonNodeFactory;
import org.codehaus.jackson.node.ObjectNode;

/**
 *
 * @author 王仲秋
 */
public class SKFNewsEntity extends BaseSendKefuMessage {

    private int articleCount;
    private List<WeixinArticle> articles;

    public SKFNewsEntity() {

    }

    public SKFNewsEntity(String openId, int articleCount, List<WeixinArticle> articles) {
        super(openId);
        this.articleCount = articleCount;
        this.articles = articles;
    }

    public int getArticleCount() {
        return articleCount;
    }

    public void setArticleCount(int articleCount) {
        this.articleCount = articleCount;
    }

    public List<WeixinArticle> getArticles() {
        return articles;
    }

    public void setArticles(List<WeixinArticle> articles) {
        this.articles = articles;
    }

    @Override
    public BaseSendKefuMessage.SendTypeEnum getMsgType() {
        return BaseSendKefuMessage.SendTypeEnum.NEWS;
    }

    public class WeixinArticle {

        private String title;
        private String description;
        private String picurl;
        private String url;

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        public String getPicurl() {
            return picurl;
        }

        public void setPicurl(String picurl) {
            this.picurl = picurl;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }

    }

    @Override
    public String toWeiXinKefuMessageJsonString() {
        ArrayNode articlesJA = new ArrayNode(JsonNodeFactory.instance);
        for (int i = 0; i < this.articleCount; i++) {
            ObjectNode jsono = new ObjectNode(JsonNodeFactory.instance);
            jsono.put("title", this.articles.get(i).title);
            jsono.put("description", this.articles.get(i).description);
            jsono.put("url", this.articles.get(i).url);
            jsono.put("picurl", this.articles.get(i).picurl);
            articlesJA.add(jsono);
        }

        ObjectNode jsono = new ObjectNode(JsonNodeFactory.instance);
        ObjectNode news = new ObjectNode(JsonNodeFactory.instance);
        news.put("articles", articlesJA);
        jsono.put("touser", this.getToUser());
        jsono.put("msgtype", getMsgType().getType());
        jsono.put(getMsgType().getType(), news);
        if (getKfAccount() != null && !getKfAccount().equals("")) {
            ObjectNode kf = new ObjectNode(JsonNodeFactory.instance);
            kf.put("kf_account", getKfAccount());
            jsono.put("customservice", kf);
        }
        return jsono.toString();
    }

}
